//
//  AttributeCollectionView.m
//  天巢新1期
//
//  Created by 唐建平 on 15/12/15.
//  Copyright © 2015年 JP. All rights reserved.
//

#import "AttributeView.h"
#import "UIView+Extnesion.h"
#define marginH 12
#define marginW 10
// 屏幕的宽
#define JPScreenW [UIScreen mainScreen].bounds.size.width
// 屏幕的高
#define JPScreenH [UIScreen mainScreen].bounds.size.height
//RGB
#define Color(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]


@interface AttributeView ()

@property (nonatomic ,weak) UIButton *btn;
@property(nonatomic,strong)void(^complect)(NSInteger index);

@end

@implementation AttributeView
- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title attributeTexts:(NSArray *)texts complect:(void(^)(NSInteger index))complect{
    self = [super initWithFrame:frame];
    if (self) {
    int count = 0;
    float btnW = 0;
    self.backgroundColor =  [UIColor clearColor];
    _complect = complect;
    UILabel *label = [[UILabel alloc]initWithFrame:flexibleFrame(CGRectMake(12, 0, 310, 15), NO)];
    label.text = [NSString stringWithFormat:@"%@",title];
    label.font = [UIFont systemFontOfSize:17*VerticalTexeRatio()];
    label.textColor = Color(50, 50, 50);
    [self addSubview:label];
    for (int i = 0; i<texts.count; i++) {
        UIButton *btn = [[UIButton alloc]init];
        btn.backgroundColor =[UIColor whiteColor];
        btn.userInteractionEnabled = YES;
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        UIColor * color = [self randomColor];
        [btn setTitleColor:color forState:UIControlStateNormal];
        [btn setTitleColor:color forState:UIControlStateSelected];
        btn.layer.borderWidth = 1;
        btn.layer.borderColor =color.CGColor;
        btn.tag = i;
        btn.titleLabel.font = [UIFont systemFontOfSize:14*VerticalTexeRatio()];
        NSString *str =texts[i];
        [btn setTitle:str forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
       
        CGSize strsize = [str sizeWithFont:[UIFont systemFontOfSize:14*VerticalTexeRatio()]];
        
        btn.width = strsize.width + 25;
        btn.height = strsize.height+ 8;
        
        if (i == 0) {
            btn.x = 11;
            btnW += CGRectGetMaxX(btn.frame);
            btn.backgroundColor =[UIColor whiteColor];
            btn.selected = YES;
            _btn = btn;
            _complect(0);
        }else{
            btnW += CGRectGetMaxX(btn.frame)+marginH;
            if (btnW*VerticalRatio() > self.frame.size.width) {
                count++;
                btn.x = 11;
                btnW = CGRectGetMaxX(btn.frame);
            }else{
                
                btn.x += btnW - btn.width;
                
            }
        }
                btn.y += count * (btn.height + 10)+27;
        
        btn.layer.cornerRadius = 4*VerticalRatio();
        btn.frame = flexibleFrame(CGRectMake(btn.x, btn.y, btn.width, btn.height), NO);
       
        [self addSubview:btn];
      
    }
        
        UIView * line = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 65+count*35, 320, 1), NO)];
        line.backgroundColor = [UIColor clearColor];
        self.frame = flexibleFrame(CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, 66+35*count), YES);
        [self addSubview:line];
}
    return self;
}

- (void)btnClick:(UIButton *)sender{
//    if (![self.btn isEqual:sender]) {
//        self.btn.backgroundColor =[UIColor whiteColor];
//        self.btn.selected = NO;
//    }
//    sender.backgroundColor = [UIColor whiteColor];
//    sender.selected = YES;
//    _complect(sender.tag);
//    self.btn = sender;
    
}

-(UIColor *) randomColor
{
    CGFloat hue = ( arc4random() % 256 / 256.0 );  //0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  // 0.5 to 1.0,away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //0.5 to 1.0,away from black
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}
@end
